package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.dao.mp.QianmiTokenDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;
import com.qianmi.open.api.DefaultOpenClient;
import com.qianmi.open.api.OpenClient;
import com.qianmi.open.api.domain.elife.OrderDetailInfo;
import com.qianmi.open.api.request.RechargeOrderInfoRequest;
import com.qianmi.open.api.response.RechargeOrderInfoResponse;

@Component
public class QianMiProvinceTask {

	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	QianmiTokenDao qianmiTokenDao;//千米token
	public static Logger log = Logger.getLogger(QianMiProvinceTask.class);
	
//	@Scheduled(fixedDelay = 60000)
	public void queryQianMiPOrder(){//省网
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = -1;//充值中
		try{
			String providerId = "0000000066";//通道ID
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(providerId);
			String linkUrl = (String) channel.get("link_url");
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			//省份
			String appKeyP = paramMap.get("appKeyP");//
			String appSecretP = paramMap.get("appSecretP");//密钥
			String tokenP = paramMap.get("tokenP");
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", providerId);//千米的运营商id
			param.put("status", 1);
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(int i = 0 , size = orderList.size() ; i < size ; i++ ){
				Map<String,Object> order = orderList.get(i);
				String orderId = (String) order.get("orderId");
				String providerOrderId = (String) order.get("providerOrderId");
				String token = tokenP;
				String appKey = appKeyP;
				String appSecret = appSecretP;
				map.put("orderId", orderId);
				map.put("providerOrderId", providerOrderId);
				OpenClient client = new DefaultOpenClient(linkUrl, appKey, appSecret);
				RechargeOrderInfoRequest req = new RechargeOrderInfoRequest();
				req.setBillId(providerOrderId);
				//String token = qianmiTokenDao.selectCurrentToken("1");
				RechargeOrderInfoResponse response = client.execute(req, token);
				if(response.getErrorCode()==null){
					OrderDetailInfo orderDetailInfo = response.getOrderDetailInfo();
					//订单充值状态 0充值中 1成功 9撤销
					String rechargeState = orderDetailInfo.getRechargeState();
					//订单付款状态 0 未付款1 已付款
					//String payState = orderDetailInfo.getPayState();
					if(rechargeState.equals("1")){//成功
						flag = 1;
					}else if(rechargeState.equals("0")){
						continue;
					}else{
						flag = 0;
					}
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			log.error("千米查询省网出错"+e.getMessage());
		}
	}
}
